package com.achao.twoway.twowaydragrecyclerview;

import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v13.view.DragStartHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "DRAG";

    CommonAdapter<String> adapter;

    private View dragView;
    private DragRecyclerViewLayout mDragRecyclerViewLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RecyclerView rv = findViewById(R.id.rv_list);
        dragView = findViewById(R.id.drag_view);
        mDragRecyclerViewLayout = findViewById(R.id.drag_layout);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 5);
        rv.setLayoutManager(gridLayoutManager);
        List<String> stringList = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            stringList.add(new String(i + ""));
        }

        adapter = new CommonAdapter<String>(this, R.layout.item_layout1, stringList) {
            @Override
            protected void convert(final ViewHolder holder, String o, int position) {
                TextView textView = holder.getView(R.id.tv_text);
                textView.setText("p:" + position);
                holder.getConvertView().setTag(position);

                mDragRecyclerViewLayout.addDragListener(new OnItemViewDragListener() {
                    @Override
                    public boolean onStartDrag(int position) {
                        return false;
                    }

                    @Override
                    public void onEnterView(int position) {
                        if (position == holder.getAdapterPosition()) {
                            holder.getConvertView().setBackgroundColor(0xff000000);
                        }
                    }

                    @Override
                    public void onViewMoving(int position) {
                    }

                    @Override
                    public void onExitView(int position) {
                        if (position != holder.getAdapterPosition()) {
                            holder.getConvertView().setBackgroundColor(0xffFF4081);
                        }
                    }

                    @Override
                    public boolean onReleaseDrag(int position) {
                        return position == 8;
                    }

                    @Override
                    public void onEndDrag(int startPosition, int currentItemPosition, boolean isPutIn) {

                    }
                });

                holder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {

                        return true;
                    }
                });

            }

        };
        rv.setAdapter(adapter);

    }
}
